mydata <- read.csv2("Athlete.csv")
Dataset description:
head(mydata, 3) #Showing first 3 units
## ID Weight Height Pressure Heart_beat Hemoglobin Hematocrit
## 1 1 78 186 115 55 143 45
## 2 2 78 185 112 54 144 55
## 3 3 77 182 119 54 146 55
## Cholesterol Glucose Gender
## 1 4.0 4.2 1
## 2 4.0 4.7 1
## 3 4.9 3.8 1
mydata$Gender <- factor(mydata$Gender, #Creating factor
levels = c(0, 1),
labels = c("F", "M"))
colnames(mydata)[5] <- "Heart_beat_per_min" #Renaming variable
summary(mydata[-1]) #Showing descriptive statistics excluding variable ID
## Weight Height Pressure Heart_beat_per_min
## Min. :55.00 Min. :166.0 Min. : 40.0 Min. :49.00
## 1st Qu.:58.00 1st Qu.:170.0 1st Qu.:100.0 1st Qu.:53.00
## Median :66.00 Median :177.0 Median :107.0 Median :54.00
## Mean :67.16 Mean :176.6 Mean :104.9 Mean :55.68
## 3rd Qu.:76.00 3rd Qu.:181.5 3rd Qu.:112.0 3rd Qu.:59.00
## Max. :79.00 Max. :187.0 Max. :128.0 Max. :64.00
##
## Hemoglobin Hematocrit Cholesterol Glucose
## Min. :140.0 Min. :42.00 Min. :3.500 Min. :3.800
## 1st Qu.:146.8 1st Qu.:48.00 1st Qu.:4.000 1st Qu.:4.600
## Median :156.0 Median :50.00 Median :4.600 Median :4.900
## Mean :155.5 Mean :51.36 Mean :4.612 Mean :5.214
## 3rd Qu.:160.0 3rd Qu.:55.00 3rd Qu.:4.900 3rd Qu.:6.000
## Max. :183.0 Max. :69.00 Max. :8.000 Max. :7.200
## NAs :1
## Gender
## F:10
## M:15
##
##
##
##
##
library(ggplot2) #Activation of library
ggplot(mydata, aes(x = Pressure)) + #Name of dataframe and axis/fills
geom_histogram(bins = 30, #Type of graph
fill = "grey90",
color = "black") +
labs(title = "Histogram of Pressure", #Title
x = "Pressure in mmHg", #x-axis name
y = "Frequency") + #y-axis name
theme_classic() #Theme
library(ggplot2)
ggplot(mydata, aes(x = "", y = Pressure)) +
geom_boxplot(fill = "grey90", color = "black") +
labs(title = "Boxplot of Pressure",
x = "",
y = "Pressure in mmHg") +
theme_classic()
library(ggplot2)
ggplot(mydata, aes(x = "", y = Pressure)) +
geom_violin(fill = "grey90", alpha = 0.6) +
geom_boxplot(width = 0.1, fill = "grey60") +
labs(title = "Violin + Boxplot of Pressure",
x = "",
y = "Pressure in mmHg") +
theme_classic()
#Removing units with missing data (drop_na, library tidyr) and excluding units with Pressure below 50. Code is written with pipes (%>%), which means "then". For using that operator, we need to activate library dplyr.
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
mydata <- mydata %>%
drop_na() %>%
filter(Pressure >= 50)
summary(mydata[-1]) #Showing descriptive statistics again after cleaning the data.
## Weight Height Pressure Heart_beat_per_min
## Min. :55.0 Min. :166.0 Min. : 90.0 Min. :49.00
## 1st Qu.:57.5 1st Qu.:170.0 1st Qu.:100.0 1st Qu.:53.00
## Median :65.0 Median :175.0 Median :109.0 Median :54.00
## Mean :66.7 Mean :176.3 Mean :107.8 Mean :55.61
## 3rd Qu.:75.5 3rd Qu.:181.2 3rd Qu.:112.0 3rd Qu.:58.50
## Max. :79.0 Max. :187.0 Max. :128.0 Max. :64.00
## Hemoglobin Hematocrit Cholesterol Glucose
## Min. :143.0 Min. :42.00 Min. :3.500 Min. :3.800
## 1st Qu.:148.5 1st Qu.:48.00 1st Qu.:4.050 1st Qu.:4.400
## Median :156.0 Median :50.00 Median :4.600 Median :4.900
## Mean :156.2 Mean :51.09 Mean :4.635 Mean :5.233
## 3rd Qu.:160.0 3rd Qu.:53.50 3rd Qu.:4.900 3rd Qu.:6.000
## Max. :183.0 Max. :69.00 Max. :8.000 Max. :7.200
## Gender
## F:10
## M:13
##
##
##
##
#Alternative function to describe the data.
library(pastecs)
##
## Attaching package: 'pastecs'
## The following object is masked from 'package:tidyr':
##
## extract
## The following objects are masked from 'package:dplyr':
##
## first, last
round(stat.desc(mydata[c(-1, -10)]), 1) #Rounding results to 1 decimal value.
## Weight Height Pressure Heart_beat_per_min Hemoglobin
## nbr.val 23.0 23.0 23.0 23.0 23.0
## nbr.null 0.0 0.0 0.0 0.0 0.0
## nbr.na 0.0 0.0 0.0 0.0 0.0
## min 55.0 166.0 90.0 49.0 143.0
## max 79.0 187.0 128.0 64.0 183.0
## range 24.0 21.0 38.0 15.0 40.0
## sum 1534.0 4054.0 2480.0 1279.0 3593.0
## median 65.0 175.0 109.0 54.0 156.0
## mean 66.7 176.3 107.8 55.6 156.2
## SE.mean 1.8 1.4 1.9 0.9 2.0
## CI.mean.0.95 3.8 2.8 3.9 1.8 4.2
## var 77.3 42.2 82.6 16.7 95.3
## std.dev 8.8 6.5 9.1 4.1 9.8
## coef.var 0.1 0.0 0.1 0.1 0.1
## Hematocrit Cholesterol Glucose
## nbr.val 23.0 23.0 23.0
## nbr.null 0.0 0.0 0.0
## nbr.na 0.0 0.0 0.0
## min 42.0 3.5 3.8
## max 69.0 8.0 7.2
## range 27.0 4.5 3.4
## sum 1175.0 106.6 120.3
## median 50.0 4.6 4.9
## mean 51.1 4.6 5.2
## SE.mean 1.2 0.2 0.2
## CI.mean.0.95 2.5 0.4 0.5
## var 32.2 0.9 1.1
## std.dev 5.7 0.9 1.0
## coef.var 0.1 0.2 0.2
#Alternative function to describe the data.
library(psych)
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
describe(mydata[c(-1, -10)])
## vars n mean sd median trimmed mad min
## Weight 1 23 66.70 8.79 65.0 66.63 11.86 55.0
## Height 2 23 176.26 6.49 175.0 176.16 8.90 166.0
## Pressure 3 23 107.83 9.09 109.0 107.32 8.90 90.0
## Heart_beat_per_min 4 23 55.61 4.09 54.0 55.42 2.97 49.0
## Hemoglobin 5 23 156.22 9.76 156.0 155.32 8.90 143.0
## Hematocrit 6 23 51.09 5.67 50.0 50.53 2.97 42.0
## Cholesterol 7 23 4.63 0.93 4.6 4.52 0.74 3.5
## Glucose 8 23 5.23 1.05 4.9 5.19 1.48 3.8
## max range skew kurtosis se
## Weight 79.0 24.0 0.06 -1.74 1.83
## Height 187.0 21.0 0.04 -1.45 1.35
## Pressure 128.0 38.0 0.31 -0.34 1.90
## Heart_beat_per_min 64.0 15.0 0.51 -0.78 0.85
## Hemoglobin 183.0 40.0 0.78 0.42 2.04
## Hematocrit 69.0 27.0 1.27 2.05 1.18
## Cholesterol 8.0 4.5 1.92 4.90 0.19
## Glucose 7.2 3.4 0.22 -1.26 0.22
mean(mydata$Cholesterol) #Calculating arithmetic mean.
## [1] 4.634783
sd(mydata$Cholesterol) #Calculating standard deviation.
## [1] 0.92717
quantile(mydata$Cholesterol, 0.30) #Calculating third decile.
## 30%
## 4.1
#Descriptive statistics by groups.
library(psych)
describeBy(x = mydata$Height,
group = mydata$Gender)
##
## Descriptive statistics by group
## group: F
## vars n mean sd median trimmed mad min max range skew
## X1 1 10 172.55 4.02 173.25 172.31 5.93 168 179 11 0.21
## kurtosis se
## X1 -1.48 1.27
## ----------------------------------------------------
## group: M
## vars n mean sd median trimmed mad min max range skew
## X1 1 13 179.12 6.7 181 179.59 5.93 166 187 21 -0.69
## kurtosis se
## X1 -1.04 1.86
#Calculating mean (FUN) for weight by gender.
aggregate(mydata$Weight ~ mydata$Gender,
FUN = mean)
## mydata$Gender mydata$Weight
## 1 F 61.20000
## 2 M 70.92308
#Histogram of height.
hist(mydata$Height, #Variable
ylab = "Number of people", #Naming of y-axis
xlab = "Height", #Naming of x-axis
main = "Distribution of height", #Heading
breaks = seq(from = 160, to = 190, by = 3)) #Breaks for histograms: sequenece from, to, by
library(ggplot2)
ggplot(mydata, aes(x = Height, fill = Gender)) + #Name of dataframe and defining axis/fills
geom_histogram(position = position_dodge(2), binwidth = 3, colour = "black") + #Type of graph
ylab("Frequency") + #y-axis name
labs(fill = "Gender") #Name of legend
library(ggplot2)
ggplot(mydata, aes(x = Height, fill = Gender)) +
geom_histogram(binwidth = 3, colour = "black") +
facet_wrap(~ Gender, ncol = 1) +
ylab("Frequency") +
labs(fill = "Gender") +
theme_minimal()
library(ggplot2)
ggplot(mydata, aes(x = Height, fill = Gender)) +
geom_histogram(binwidth = 3, colour = "black") +
facet_grid(Gender ~ .) +
ylab("Frequency") +
labs(fill = "Gender") +
theme_minimal()
library(ggplot2)
ggplot(mydata, aes(y = Weight, x = Height)) +
geom_point()
library(ggplot2)
ggplot(mydata, aes(x = Gender, y = Height, fill = Gender)) +
geom_violin(fill = "grey85", colour = "black", alpha = 1) + # light grey
geom_boxplot(width = 0.1, fill = "grey40", colour = "black") + # darker grey
scale_fill_grey(start = 0.4, end = 0.8) + # ensures greyscale mapping
labs(title = "Violin + Boxplot of Height by Gender",
x = "Gender",
y = "Height") +
theme_bw() +
theme(legend.position = "none") #legend unnecessary with clear facets
#95% confiedence interval for the arithmetic mean of heart beat per minute.
ybar <- mean(mydata$Heart_beat_per_min)
sd <- sd(mydata$Heart_beat_per_min)
n <- nrow(mydata)
se <- sd/sqrt(n)
ybar_lower_t = ybar + qt(0.025, df = n-1) * se
ybar_upper_t = ybar + qt(0.975, df = n-1) * se
cat(c("95-% CI:", round(ybar_lower_t, 3), "< Mu <", round(ybar_upper_t, 3)))
## 95-% CI: 53.841 < Mu < 57.376
# 95% CI for the mean of heartbeats per minute
ybar <- mean(mydata$Heart_beat_per_min)
s <- sd(mydata$Heart_beat_per_min)
n <- nrow(mydata)
se <- s / sqrt(n)
t_crit <- qt(0.975, df = n - 1)
CI <- ybar + c(-1, 1) * t_crit * se
names(CI) <- c("Lower 95% CI", "Upper 95% CI")
CI
## Lower 95% CI Upper 95% CI
## 53.84134 57.37605
t.test(mydata$Heart_beat_per_min, conf.level = 0.95)$conf.int
## [1] 53.84134 57.37605
## attr(,"conf.level")
## [1] 0.95
#Comparing 95% confidence interval for arithmetic mean of blood pressure between male and female.
library(Rmisc)
## Loading required package: lattice
## Loading required package: plyr
## --------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## --------------------------------------------------------------------
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, mutate, rename, summarise, summarize
statistics <- summarySE(mydata,
measurevar = "Pressure",
groupvars = "Gender",
conf.interval = 0.95)
library(ggplot2)
ggplot(statistics, aes(x = Gender, y = Pressure)) +
geom_point(size = 3, color = "red") +
geom_errorbar(aes(ymin = Pressure - ci, ymax = Pressure + ci), width = 0.2) +
xlab("Gender")